# coding:utf-8
__author__ = 'golker'

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

from flask_restful import Resource, fields, reqparse
from ..models import Comments
import datetime
from ..extensions import db

parser = reqparse.RequestParser()
parser.add_argument('name', type=str, help='名字不能为空')
parser.add_argument('comment', type=str, location='form', help='评论内容不能为空')
parser.add_argument('parent_id', type=int, help='被评论id不能为空')
parser.add_argument('article_id', type=int, help='不能为空')
parser.add_argument('user_id', type=int)

comment_field = {
    'name': fields.String,
    'comment': fields.String,
    'comment_on': fields.Integer,
    'article_id': fields.Integer,
    'user_id': fields.Integer,
}


class Comment(Resource):
    # decorators = [csrf.exempt]

    def post(self):
        args = parser.parse_args()
        comment = Comments(name=args.name,
                           comment=args.comment,
                           parent_id=args.parent_id,
                           article_id=args.article_id,
                           user_id=args.user_id,
                           timestamp=datetime.datetime.now()
                           )
        db.session.add(comment)
        db.session.commit()
        return {'status': '1',
                'msg': 'sucess',
                'error': '0'}
